package com.blockmeta.bbs.businesslibrary.util;

import android.content.Context;
import android.os.Handler;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.pojo.RefreshJWTPOJO;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.retrofit.api.InterfaceGetPublicKey;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.pojo.LoginPOJO;
import com.blockmeta.bbs.businesslibrary.pojo.UserPhonePOJO;

import io.reactivex.subscribers.ResourceSubscriber;

import static com.blockmeta.bbs.businesslibrary.launch.ResourceDownload.MSG_GET_BUNDLE;

public class JWTRefreshUtil {

    public static void refreshJWT(final Context context, final Handler handler) {

        String token = AccountHelper.getToken();
        int uidInt = AccountHelper.getUidInt();
        if(StringUtils.isEmpty(token) || uidInt == 0){
            handler.sendEmptyMessage(MSG_GET_BUNDLE);
            return;
        }
        RefreshJWTPOJO refreshJWTPOJO = new RefreshJWTPOJO();
        refreshJWTPOJO.setJwt(token);
        refreshJWTPOJO.setUserId(uidInt);
        RetrofitFactory.getRootRetrofit().create(InterfaceGetPublicKey.class)
                .refreshToken(refreshJWTPOJO)
                .compose(RxUtil.dataNullResult())
                .compose(RxUtil.<BaseImResponse<LoginPOJO>>rxSchedulerHelper())

                .retryWhen(new RxUtil.RetryWithDelay(2, 2000))
                .subscribe(new ResourceSubscriber<BaseImResponse<LoginPOJO>>() {
                    @Override
                    public void onNext(BaseImResponse<LoginPOJO> pojo) {
                        if("200".equals(pojo.code)){
                            if(pojo.data == null){
                                AccountHelper.doLoginOut();
                            }else {
                                saveUserInfo(pojo.data);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        LogUtils.e("author", "刷新jwt失败");
                    }

                    @Override
                    public void onComplete() {
                        handler.sendEmptyMessage(MSG_GET_BUNDLE);
                    }
                });
    }

    public static void refreshJWT() {

        String token = AccountHelper.getToken();
        int uidInt = AccountHelper.getUidInt();
        if(StringUtils.isEmpty(token) || uidInt == 0){
            return;
        }
        RefreshJWTPOJO refreshJWTPOJO = new RefreshJWTPOJO();
        refreshJWTPOJO.setJwt(token);
        refreshJWTPOJO.setUserId(uidInt);
        RetrofitFactory.getRootRetrofit().create(InterfaceGetPublicKey.class)
                .refreshToken(refreshJWTPOJO)
                .compose(RxUtil.dataNullResult())
                .compose(RxUtil.<BaseImResponse<LoginPOJO>>rxSchedulerHelper())
                .retryWhen(new RxUtil.RetryWithDelay(2, 2000))
                .subscribe(new ResourceSubscriber<BaseImResponse<LoginPOJO>>() {
                    @Override
                    public void onNext(BaseImResponse<LoginPOJO> pojo) {
                        if("200".equals(pojo.code)){
                            if(pojo.data == null){
                                AccountHelper.doLoginOut();
                            }else {
                                saveUserInfo(pojo.data);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        LogUtils.e("author", "刷新jwt失败");
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private static void saveUserInfo(LoginPOJO loginPOJO) {
        if (loginPOJO.userInfo==null){return;}
        UserPhonePOJO userPhonePOJO = new UserPhonePOJO();
        userPhonePOJO.setLogo(loginPOJO.userInfo.logo);
        userPhonePOJO.setEmail(loginPOJO.userInfo.email);
        userPhonePOJO.setMobile(loginPOJO.userInfo.mobile);
        userPhonePOJO.setUid(loginPOJO.userInfo.uid);
        LogUtils.d("author saveUserInfo" + loginPOJO.userInfo.uid);
        userPhonePOJO.setUserName(loginPOJO.userInfo.userName);
        userPhonePOJO.setToken(loginPOJO.token);

        AccountHelper.doLogin(userPhonePOJO);

    }
}
